<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'海报列表'),
	array('op'=>'edit','title'=>'添加海报'),	
);
if(!$this->plugin_exists('jy_poster')){
	exi('请先安装《超级海报》！','error');
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['plugin']="jy_renwu";
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','isdefault','follows','times','type');
	$list=pdo_getslice('jy_poster_poster',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['url']=custom_url('jy_poster/build//id='.$item['id'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：
		1、如果添加商品类的海报，商品详情页会有海报按钮；关键词触发商品类的海报，可能会不显示商品图片，名称等；</br>
		[常见问题]：海报生成了图片不显示?1、可能是服务器的SSL证书版本异常！2、openssl未开启；3、PHP的curl异常,直接换个PHP版本试试（建议5.6以上）</br>
		<red>海报不生效</red>？一般都是缓存问题，可以先点击清空海报缓存，然后还得清空手机缓存！
		<red>如何清空关注二维码</red>？移步《我的端》--》二维码管理--删除
		','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('name'=>'times','title'=>'扫描数','type'=>'string'),
			array('name'=>'follows','title'=>'关注数','type'=>'string'),
			array('name'=>'url','title'=>'海报效果','type'=>'ewm'),
			array('name'=>'url','title'=>'海报链接','type'=>'copy'),
			array('name'=>'isdefault','title'=>'是否默认','type'=>'radio','options'=>'isdefault'),
		),
		'action'=>array(
			array('op'=>'sj','title'=>'设计海报','type'=>'href'),
			array('op'=>'qc','title'=>'清除海报缓存','type'=>'href'),
			
		)
		),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$return['type']=array('1'=>'商城','2'=>'小店(多商户)','3'=>'商品','4'=>'分销(小店)','5'=>'自定义页面','6'=>'关注海报');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回海报列表'),
		),
		'edits'=>array(
			array(
					'title'=>'基础设置',
					'list'=>array(
					array('name'=>'title','title'=>'海报名称','type'=>'string'),
					array('name'=>'type','title'=>'海报类型','type'=>'radio','options'=>'types'),
					array('name'=>'keyword','title'=>'回复关键字','type'=>'string'),
					array('name'=>'isdefault','title'=>'是否默认','type'=>'radio','options'=>'isdefault','help'=>'如果是分销小店，需开启默认，才会在分销客的小店二维码生效！'),
					array('name'=>'fenxiao_level','title'=>'扫码关注成为下线','type'=>'switch','show'=>array('type'=>array(4,6)),'help'=>'新用户扫关注成为其下线，不受分销《分销设置》成为下线的限制！'),
					array('name'=>'fenxiao_set','title'=>'扫码关注成为分销商','type'=>'switch','show'=>array('type'=>array(4,6)),'help'=>'新用户扫关注成为分销商，不受分销《分销设置》成为分销商的限制！'),
					array('name'=>'ewmurl','title'=>'二维码链接','type'=>'link','from'=>'action','show'=>array('type'=>5),'help'=>'自定义页面类型有效；目前所有的DIY页面，只要页面开启了检查登录，都能实现点击绑定下级；其他部分首页可能不会绑定下级，除非该会员已经登录！'),
					array('name'=>'waittext','title'=>'等待生成文字','type'=>'string','help'=>'例如：您的专属海报正在拼命生成中，请等待片刻...'),
					array('name'=>'reccredit','title'=>'推荐人获得积分','type'=>'string'),
					array('name'=>'recmoney','title'=>'推荐人获得现金','type'=>'string'),					
					array('name'=>'subcredit','title'=>'关注人获得积分','type'=>'string','show'=>array('type'=>6)),
					array('name'=>'submoney','title'=>'关注人获得现金','type'=>'string','show'=>array('type'=>6)),
					array('name'=>'templateid','title'=>'通知模板消息ID（任务处理通知）','type'=>'string','help'=>'公众平台模板消息ID: OPENTM200605630，如果不填写，则使用客服消息发送通知'),
					array('name'=>'recpaycontent','title'=>'推荐者通知','type'=>'string','help'=>'例如：[nickname] 通过您的二维码关注了公众号! 获得了 [credit] 个积分,[money]元奖励![nickname] 为扫码者昵称 [credit] 奖励的积分 [money] 奖励的钱 '),					
					array('name'=>'subpaycontent','title'=>'关注者通知','type'=>'string','show'=>array('type'=>6),'help'=>'例如：您扫描了 [nickname] 的二维码关注了公众号! 获得了, [credit] 个积分,[money]元奖励![nickname] 为推荐者昵称 [credit] 奖励的积分 [money] 奖励的钱 '),
					array('name'=>'entrytext','title'=>'关注者现金奖励入账描述','type'=>'string','show'=>array('type'=>6),'help'=>'默认为：您通过 [nickname]的推广二维码扫码关注的奖励,[nickname]为推荐者昵称'),
					array('name'=>'resptext','title'=>'推荐者现金奖励入账描述','type'=>'string','help'=>'默认为：推荐 [nickname] 扫码关注的奖励,[nickname]为扫码者昵称'),	
		)))
	);
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$return['types']=array(1=>'商城',2=>'小店(多商户)',3=>'商品',4=>'分销(小店)',5=>'自定义页面','6'=>'关注海报');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写标题','error');
	}
	if(!$postdata['type']){
		exi('请选择类型','error');
	}
	if($postdata['type']==5 && !$postdata['ewmurl']){
		exi('自定义页面，必须填写二维码链接','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'keyword'=>trim( $postdata['keyword']),
		'type'=>intval($postdata['type']),
		'isdefault'=>$postdata['isdefault'],
		'waittext'=>trim( $postdata['waittext']),
		'reccredit'=>intval($postdata['reccredit']),
		'recmoney'=>intval($postdata['recmoney']),
		'subcredit'=>intval($postdata['subcredit']),
		'submoney'=>intval($postdata['submoney']),
		'templateid'=>trim( $postdata['templateid']),
		'recpaycontent'=>trim($postdata['recpaycontent']),
		'subpaycontent'=>trim($postdata['subpaycontent']),
		'entrytext'=>trim($postdata['entrytext']),
		'resptext'=>trim($postdata['resptext']),
		'ewmurl'=>trim($postdata['ewmurl']),
		'fenxiao_level'=>intval($postdata['fenxiao_level']),
		'fenxiao_set'=>intval($postdata['fenxiao_set']),
	);
	if($data['isdefault']){
			pdo_update('jy_poster_poster',array('isdefault'=>0),array('uniacid'=>$_SESSION['uniacid']));
	}
	$id=intval($postdata['id']);
	if($id){
		$da=array('backtype'=>8,'key'=>$id,'status'=>1,'title'=>'海报'.$id,'event'=>'create','keyword'=>trim( $postdata['keyword']));
		cfc('core')->save_back($da,'jy_poster');
		pdo_update('jy_poster_poster',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['plugin']='jy_renwu';
		pdo_insert('jy_poster_poster',$data);
		$id=pdo_insertid();
		$da=array('backtype'=>8,'key'=>$id,'status'=>1,'title'=>'海报'.$id,'event'=>'create','keyword'=>trim( $postdata['keyword']));
		cfc('core')->save_back($da,'jy_poster');
	}
	
	exi('操作成功！','','list');
}
if($_RQ['op']=='sj'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['data']=$item['data']?json_decode($item['data'],true):array('data'=>array());
	}else{
		$item=array();
		$item['data']=array('data'=>array());
	}	
	foreach($item['data']['data'] as &$d){
		$d = model('jy_poster/model')->getitemData($d);
	}
	$item['image']=$item['data']['image'];
	$return['datatypes']=$this->datatypes;
	$return['datapushs']=$this->datapushs;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='qc'){
	$id=intval($_RQ['id']);
	$path=ROOT_AT .'oss/' . $_SESSION['uniacid'].'/images/jy_poster/'.$id.'/';
	$handle = scandir($path);
	foreach($handle as $val){
    	//排除目录中的.和..
    	if($val !="." && $val !=".."){
    		//如果是文件直接删除
    		unlink($path.$val);
		}
	}
	$like='oss/' . $_SESSION['uniacid'].'/images/jy_poster/'.$id.'/';
    pdo_delete('core_attachment',array('attachment like'=>'%'.trim($like).'%','uniacid'=>$_SESSION['uniacid']));
	exi('清除成功','','list');
}
if($_RQ['op']=='design'){
	$postdata=get_postdata();
	$id=$postdata['id'];
	if(!$id){
		exi('请选择海报','error');
	}
	$data['data']=$postdata['data']['data'];
	$data['image']=$postdata['data']['image'];
	pdo_update('jy_poster_poster',array('data'=>json_encode($data)),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('保存成功','error','list');
}

if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_poster_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
